<?php
namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class GoodsPrice extends Model
{

    const TABLE_NAME = "goods_price";

    protected $table = 'goods_price';

    protected $primaryKey = 'goods_price_id';

    protected $GoodsPrice = self::TABLE_NAME;

    public $timestamps = false;

    public function delGoodsPrice($goods_price_id) // 删
    {
        $sql = 'update goods_price set delete_flg = 1 where goods_price_id=?';
        $param = [
            $goods_price_id
        ];
        $delflag = DB::update($sql, $param);
        return $delflag;
    }

//     public function updGoodsPrice($value, $field, $arr) // 改
//     {
//         $GoodsPrice = $this->where($field, '=', $value);
//         return $GoodsPrice->update($arr);
//     }
    public function updGoodsPrice($goodsPriceInfo) // 改
    {
        $sql = 'update goods_price set goods_id = ?, invoice_rate = ?, invoice_price = ?, corp_id = ? where goods_price_id=?';
        $param = [
                $goodsPriceInfo['goods_id'],
                $goodsPriceInfo['invoice_rate'],
                $goodsPriceInfo['invoice_price'],
                $goodsPriceInfo['corp_id'],
                $goodsPriceInfo['goods_price_id']
           ];
        $flag = DB::update($sql, $param);
        return $flag;
    }

    public function addGoodsPrice($goodsPriceInfo) // 增
    {

          $sql = 'INSERT INTO  goods_price (corp_id, goods_id, invoice_rate, invoice_price, delete_flg, created_at, updated_at) VALUES (?,?,?,?,0,now(),now())';
          $param = [
                      $goodsPriceInfo['corp_id'],
                      $goodsPriceInfo['goods_id'],
                      $goodsPriceInfo['invoice_rate'],
                      $goodsPriceInfo['invoice_price']
                   ];
          return DB::insert($sql, $param);
    }

    public function getGoodsPriceInfoByPriceId($price_id)
    {
        $sql = 'select * from goods_price gp
               inner join goods_master gm
               on gm.goods_id = gp.goods_id
               inner join category_master cm on gm.category_id=cm.category_id
               where gp.delete_flg =0 and gm.delete_flg=0 and cm.delete_flg=0
               and goods_price_id=?';
        $param = [
            $price_id,
        ];
        $users = DB::select($sql, $param);
        return $users;
    }

    public function getGoodsPrice(){
        $sql =' select *
                from goods_master,corp_master,goods_price,category_master
                where goods_master.goods_id = goods_price.goods_id
                and corp_master.corp_id = goods_price.corp_id
                and category_master.category_id=goods_master.category_id
                and goods_master.delete_flg =0
                and corp_master.delete_flg =0
                and goods_price.delete_flg =0';

        $price =DB::select($sql);

        return $price;
    }
    public function getGoodsPriceByCorpId($corp_id){
        $sql =' select *
                from goods_master,corp_master,goods_price,category_master
                where goods_master.goods_id = goods_price.goods_id
                and corp_master.corp_id = goods_price.corp_id
                and category_master.category_id=goods_master.category_id
                and goods_master.delete_flg =0
                and corp_master.delete_flg =0
                and goods_price.delete_flg =0
                and corp_master.corp_id =?';

        $param = [
            $corp_id,
        ];

        $price =DB::select($sql, $param);

        return $price;
    }

    public function getCateCode()
    {
        $sql = 'select category_id,category_name from category_master  WHERE delete_flg = 0 ';
        $cataCode = DB::select($sql);
        return $cataCode;
    }

    public function getGoodsCodeByCateId($CateId)
    {
//         $sql = 'select * from goods_master gm
//                 left join category_master cm on cm.category_id =gm.category_id
//                 WHERE cm.delete_flg = 0 and gm.delete_flg=0 and cm.category_id =? ';

        $sql = 'select *
                from goods_master,corp_master,goods_price,category_master
                where goods_master.goods_id = goods_price.goods_id
                and corp_master.corp_id = goods_price.corp_id
                and category_master.category_id=goods_master.category_id
                and goods_master.delete_flg =0
                and corp_master.delete_flg =0
                and goods_price.delete_flg =0
                and category_master.category_id=?';
        $param = [
            $CateId,
        ];
        $goodsCode = DB::select($sql,$param);
        return $goodsCode;
    }
    public function getGoodsCode()
    {
        $sql = 'select * from goods_master gm
                left join category_master cm on cm.category_id =gm.category_id
                WHERE cm.delete_flg = 0 and gm.delete_flg=0';

        $goodsCode = DB::select($sql);
        return $goodsCode;
    }
    public function getCorpName()
    {
        $sql = 'select corp_id, corp_name from corp_master where delete_flg=0';
        $corpName = DB::select($sql);
        return $corpName;
    }

}
